/*
 * Copyright 2009-now Nobject.org, All Rights Reserved.
 * 
 * This software is licensed under LGPL license.
 * See terms of license at gnu.org.
 * 
 * For more information,visit:
 * 
 * http://www.nobject.org
 */
package org.nobject.common.buffer;

import java.io.UnsupportedEncodingException;

/**
 * 字节缓冲类
 *
 * @author bianrongjun
 * @version 1.0
 */
public class ByteBuffer {

	int point = -1;
	int cacheSize = 1024;
	byte[] cacheBytes = null;

	/**
	 * 
	 */
	public ByteBuffer() {
		cacheBytes = new byte[cacheSize];
	}

	/**
	 * 构造函数 
	 * @param cacheSize
	 */
	public ByteBuffer(int cacheSize) {
		this.cacheSize=cacheSize;
		cacheBytes = new byte[this.cacheSize];
	}
	
	/**
	 * append
	 * 
	 * @param b
	 */
	public ByteBuffer append(byte b) {
		if (point++ >= (cacheBytes.length - 1)) {
			byte[] exchangeCache = cacheBytes;
			cacheBytes = new byte[exchangeCache.length + cacheSize];
			System.arraycopy(exchangeCache, 0, cacheBytes, 0, exchangeCache.length);
		}
		cacheBytes[point] = b;
		return this;
	}
	
	/**
	 * append
	 * @param b
	 * @param startIndex
	 * @param count
	 */
	public ByteBuffer append(byte [] b,int startIndex,int count) {
		int c=0;
		for (int i = 0; i < b.length; i++) {
			if(i>=startIndex&&c<count){
				append(b[i]);
				c++;
			}
		}
		return this;
	}
	
	/** 
	 * append
	 * @param b
	 * @return
	 */
	public ByteBuffer append(byte [] b) {
		for (int i = 0; i < b.length; i++) {
			append(b[i]);
		}
		return this;
	}
	
	/**
	 * append
	 * @param s
	 */
	public ByteBuffer append(String s){
		if(s!=null){
			byte [] bs=s.getBytes();
			for (int i = 0; i < bs.length; i++) {
				append(bs[i]);
			}
		}
		return this;
	}
	
	/**
	 * getBytes
	 * 
	 * @return
	 */
	public byte[] getBytes() {
		byte[] b=new byte[point + 1];
		System.arraycopy(cacheBytes, 0, b, 0, point + 1);
		return b;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new String(cacheBytes, 0, point + 1);
	}

	/**
	 * length
	 * 
	 * @return
	 */
	public int length() {
		return point + 1;
	}

	/**
	 * toString
	 * 
	 * @param charset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String toString(String charset) throws UnsupportedEncodingException {
		return new String(getBytes(), charset);
	}
	
	/**
	 * byteString
	 * @param spliter
	 * @return
	 */
	public String byteString(String spliter){
		StringBuffer sb=new StringBuffer();
		for (int i = 0; i < point + 1; i++) {
			sb.append(cacheBytes[point]);
			if(spliter!=null) sb.append(spliter);
		}
		return sb.toString();
	}
	
	/** 
	 * byteAt
	 * @param idx
	 * @return
	 */
	public byte byteAt(int idx){
		if(idx>point) idx=point;
		return cacheBytes[idx];
	}
	
	/** 
	 * lastByte
	 * @return
	 */
	public byte lastByte(){
		return cacheBytes[point];
	}
	
	/** 
	 * 清空
	 */
	public void clear(){
		cacheBytes = new byte[cacheSize];
	}
}
